package com.i5.distributedlock.core.key.strategy;

import com.i5.distributedlock.constant.LockCommonConstant;
import com.i5.distributedlock.core.key.LockKey.Builder;
import com.i5.distributedlock.enumeration.LockScheme;
import com.i5.distributedlock.exception.KeyBuilderException;

import java.lang.reflect.Method;

/**
 * @BelongsPackage: com.i5.distributedlock.core.strategy
 * @Author: mc0710
 * @CreateTime: 2019-04-17 10:01
 * @Description:
 */
public abstract class KeyStrategy {

    protected LockScheme scheme;
    protected String className;
    protected String methodName;
    protected Method realMethod;
    protected Object[] args;
    protected String prefixName;

    public KeyStrategy(LockScheme scheme, String className, String methodName, Method realMethod, Object[] args, String prefixName) {
        this.scheme = scheme;
        this.className = className;
        this.methodName = methodName;
        this.realMethod = realMethod;
        this.args = args;
        this.prefixName = prefixName;
    }

    public String getSimpleClassName() {
        return this.className.split("\\.")[this.className.split("\\.").length - 1];
    }

    /**
     * 包装锁的key标记
     *
     * @param valTag
     * @return
     */
    public String wrapKeyTag(String valTag) {
        return getSimpleClassName() + LockCommonConstant.KEY_SPLIT_MARK + this.methodName
                + LockCommonConstant.KEY_SPLIT_MARK + valTag;
    }

    /**
     * 添加锁
     */
    public abstract Builder generateBuilder() throws KeyBuilderException;

}
